package com.fastPlat.scheduled;

import java.util.Date;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.exception.RemotingException;

@Component
public class MessageProducer {
	
	@Autowired
	private DefaultMQProducer rocketmqProducer;
	
	@Value("${rocketMQ-topic}")
	private String topic;
	
	@Value("${rocketMQ-tags}")
	private String tags;
	
	public void sendMessage(String msg,String keys) {
        try {
        	System.out.println("topic:["+topic+"],tags:["+tags+"]");
            Message message = new Message(topic,tags,msg.getBytes());
            message.setKeys(keys);
            SendResult sendResult = rocketmqProducer.send(message);
            System.out.println(new Date()+"-发送消息为：id:" + sendResult.getMsgId() + " result:" + sendResult.getSendStatus());
        } catch (BeansException e) {
            e.printStackTrace();
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
